<?php
require_once "lib/Controller.php";

class user extends Controller
{
    public function index()
    {
        session_start();
        if (empty($_COOKIE["token"])) {
            exit("<script>location.href='index.php?c=user/login'</script>");
        }
        $date = $_SESSION[$_COOKIE["token"]];
        $date = getinfo($date['id']);
        if (empty($date))
            exit("系统出错！");
        $date['title'] = "个人资料";
        if(empty($date['name']))
            $date['name'] = $date['username'];
        setcookie("token", $_COOKIE["token"], time() + 120);
        $this->render("index", $date);
    }

    public function login()
    {
        $this->render();
    }

    public function register()
    {
        $this->render();
    }

    public function userdo()
    {
        @$type = $_POST['type'];
        @$user = $_POST['user'];
        @$pasw = $_POST['pasw'];
        @$code = $_POST['code'];
        @$name = $_POST['name'];
        @$img = $_POST['img'];
        @$remarks = $_POST['remarks'];
        @$id = $_POST['id'];
        switch ($type) {
            case "isname":
                if (!isname($user))
                    echo 0;
                else
                    echo 1;;
                break;
            case "login":
                session_start();
                if(!VCode($code)){
                    echo 2;
                    return;
                }
                $date = login($user, $pasw);
                if ($date) {
                    $token = md5(time());
                    setcookie("token", $token, time() + 60);
                    $_SESSION[$token] = $date;
                    echo 0;
                } else
                    echo 1;;
                break;
            case "register":
                if (!isname($user) && register($user, $pasw))
                    echo 0;
                else
                    echo 1;;
                break;
            case "update":
                if(empty($_FILES["img"]["tmp_name"]))
                    $img = "default.png";
                else{
                    $img = md5($_FILES["img"]["tmp_name"]).".png";
                    move_uploaded_file($_FILES["img"]["tmp_name"],"public/img/portrait/" . $img);
                }
                if(setinfo($id,$name,$remarks,$img))
                    echo 0;
                else
                    echo 1;
                break;
            case "select":
                $info = getinfo($id);
                if($info){
                    $info['state'] = 0;
                    echo json_encode($info);
                }
                else
                    echo '{"state":1}';
                ;break;
            case "delete":
                if(delete($id))
                    echo 0;
                else
                    echo 1;
                ;break;
            case "out":
                out();
                echo 0;;
                break;
        }

    }

    public function verification()
    {
        session_start();
        function random($len)
        {
            $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
            mt_srand();
            $strs = "";
            for ($i = 0; $i < $len; $i++) {
                $strs .= $srcstr[mt_rand(0, 30)];
            }
            return $strs;
        }
        //随机生成的字符串
        $str = random(5);
        //验证码图片的宽度
        $width = 60;
        //验证码图片的高度
        $height = 34;
        //声明需要创建的图层的图片格式
        @header("Content-Type:image/png");
        //创建一个图层
        $im = imagecreate($width, $height);
        //背景色
        $back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
        //模糊点颜色
        $pix = imagecolorallocate($im, 187, 230, 247);
        //字体色
        $font = imagecolorallocate($im, 41, 163, 238);
        //绘模糊作用的点
        mt_srand();
        for ($i = 0; $i < 1000; $i++) {
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
        }
        //输出字符
        imagestring($im, 5, 8, 8, $str, $font);
        //输出矩形
        imagerectangle($im, 0, 0, $width - 1, $height - 1, $font);
        //输出图片
        imagepng($im);
        imagedestroy($im);
        $str = md5($str);
        //选择 cookie
        //SetCookie("verification", $str, time() + 7200, "/");
        //选择 Session
        $_SESSION["verification"] = $str;
    }
}